package user

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserapiLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserapiLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserapiLoginLogic {
	return &UserapiLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserapiLoginLogic) UserapiLogin(req *types.UserLoginReq) (resp *types.UserLoginResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.Login(ctx, &usercenter.LoginReq{
		Name:     req.Name,
		Password: req.Password,
	})
	if err != nil {
		return nil, err
	}
	resp = new(types.UserLoginResp)
	resp.Access = res.Access
	resp.Refresh = res.Refresh
	return
}
